package com.lanyuan.a.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lanyuan.a.atest.FoodFormMap;
import com.lanyuan.a.bean.Comment;
import com.lanyuan.a.bean.Food;
import com.lanyuan.a.dao.BaseDao;
import com.lanyuan.a.service.CommentService;
import com.lanyuan.a.service.FoodService;
import com.lanyuan.plugin.PageView;
@Service("commentService")
public class CommentServiceImpl implements CommentService {
	@Autowired
	public BaseDao<Comment> baseDao;

	@Override
	public List<Comment> list() {
		return baseDao.find("from Comment");
	}

	@Override
	public void deleteCommentByIds(String[] ids) {
		Integer[] id = new Integer[ids.length];
		for(int i=0 ; i<ids.length; i++){
			System.out.println(ids[i]);
			id[i] = Integer.parseInt(ids[i]);
		}
		baseDao.delete("delete from Comment u where u.id in (:ids)","ids" , id);
	}

	@Override
	public List<Comment> queryByPage(FoodFormMap formMap) {
		Long count = null;
		String ss = (String)formMap.get("name");
		PageView pageView = (PageView)formMap.get("paging");
		int page = pageView.getPageNow();
		int rows = pageView.getPageSize();
		if(ss != null && !ss.equals("")){
			String hql = "from Food f where name like ?";
			String countHql = "select count (1) from Food f where name like ?";
			count = baseDao.getCountByConditionLike(countHql , new Object[]{ss});
			pageView.setRowCount(count);
			return baseDao.find(hql, new Object[]{"%"+ss+"%"}, page, rows);
		}else{
			String countHql = "select count (1) from Food f";
			count = baseDao.count(countHql);
			pageView.setRowCount(count);
			return baseDao.find("from Food f", new Object[]{}, page , rows);
		}
	}

	@Override
	public void saveComment(Comment comment) {
		baseDao.saveOrUpdate(comment);
	}

	@Override
	public void delByIds(String[] ids) {
		Integer[] id = new Integer[ids.length];
		for(int i=0 ; i<ids.length; i++){
			System.out.println(ids[i]);
			id[i] = Integer.parseInt(ids[i]);
		}
		baseDao.delete("delete from Comment u where u.id in (:ids)","ids" , id);
	
	}
	

}
